package com.yy.okhttp;

import com.yy.okhttp.chain.ConnectionInterceptor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: gongyongfeng2@yy.com
 * Date: 2020/7/29 15 :18
 */
public class InterceptorChain {
    private List<Interceptor> mInterceptors = new ArrayList<>();
    private int index;
    private Call mCall;

    public InterceptorChain(List<Interceptor> interceptors, int index, Call call) {
        mInterceptors = interceptors;
        this.index = index;
        mCall = call;
    }

    public Response proceed() throws IOException {
        return proceed();
    }

    public Response proceed(ConnectionInterceptor connection)
            throws IOException {
        Interceptor interceptor = mInterceptors.get(index);
        InterceptorChain next = new InterceptorChain(mInterceptors, index + 1, mCall, connection);
        Response response = interceptor.intercept(next);
        return response;
    }

    public Call call() {
        return mCall;
    }
}
